package com.lzymall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.lzymall.common.ServiceResponse;
import com.lzymall.dao.CartMapper;
import com.lzymall.dao.CategoryMapper;
import com.lzymall.pojo.Category;
import com.lzymall.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @Author: lizhenyao
 * @Desciption:
 */
@Service("iCategoryService")
@Slf4j
public class CategoryServiceImpl implements ICategoryService{

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public ServiceResponse addCategory(String categoryName, Integer parentId) {
        if (parentId == null || StringUtils.isBlank(categoryName)){
            return ServiceResponse.createByErrorMessage("添加品类的参数错误");
        }

        //添加品类
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);
        int resultCount = categoryMapper.insertSelective(category);
        if (resultCount > 0){
            return ServiceResponse.createBySuccessMessage("添加品类成功");
        }
        return ServiceResponse.createByErrorMessage("添加失败");
    }

    @Override
    public ServiceResponse updateCategoryName(Integer categoryId, String categoryName) {
        if (categoryId == null || StringUtils.isBlank(categoryName)){
            return ServiceResponse.createByErrorMessage("更新品类的参数错误 ");
        }

        //更新操作
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);
        int rowCount = categoryMapper.updateByPrimaryKeySelective(category);
        if (rowCount > 0){
            return ServiceResponse.createBySuccessMessage("更新品类成功");
        }
        return ServiceResponse.createByErrorMessage("更新失败");
    }

    @Override
    public ServiceResponse<List<Category>> getChildrenParallelCategory(Integer categoryId) {
        List<Category> list = categoryMapper.selectCategoryChildenByParentId(categoryId);
        if (CollectionUtils.isEmpty(list)){
            log.info("未找到当前分类的子分类");
        }
        return ServiceResponse.createBySuccess(list);
    }

    @Override
    public ServiceResponse<List<Integer>> getCategoryAndDeepChildrenCategory(Integer categoryId) {
        Set<Category> categorySet = Sets.newHashSet();
        findChildCategory(categorySet, categoryId);

        List<Integer> categoryIdList = Lists.newArrayList();
        for (Category item : categorySet){
            categoryIdList.add(item.getId());
        }
        return ServiceResponse.createBySuccess(categoryIdList);
    }

    /**
     * 递归查找所有的子节点
     * @param categories
     * @param categoryId
     * @return
     */
    private Set<Category> findChildCategory(Set<Category> categories, Integer categoryId){
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if (categoryId != null){
            categories.add(category);
        }
        List<Category> list = categoryMapper.selectCategoryChildenByParentId(categoryId);
        for (Category item : list){
            findChildCategory(categories, item.getId());
        }
        return categories;
    }
}
